export function elValidate(self, name) {
  return new Promise(function (resolve, reject) {
    self.$refs[name].validate((valid) => {
      if (valid) {
        resolve(valid)
      } else {
        reject('表单验证不通过')
      }
    })
  })
}

const toSet = (self, attr, value) => {
  let [first, seconds] = attr.split(/\.(?=\w+$)/)
  first = eval(`self.${first}`)
  self.$set(first, seconds, value)
}
const toGet = (self, attr) => eval('self.' + attr)

/*------ computed 映射 begin --------*/


/*
* 把 元 转为 分并映射到指定属性上
*  @param {string}
* */
export const moneyMap = attr => {
  return {
    get() {
      let v = toGet(this, attr)
      if (v !== '') {
        v = isNaN(v) ? 0 : v / 100
      }
      return v
    },
    set(v) {
      if (v !== '') {
        v = isNaN(v) ? 0 : v * 100
      }
      toSet(this, attr, v)
    }
  }
}


/*
* 把日期范围数组 拆成两个属性并映射到指定属性上
* @param {string} 开始时间
* @param {string} 结束时间
* */
export const timeMap = (dateStartStr, dateEndStr) => {
  return {
    get() {
      const dateStart = toGet(this, dateStartStr)
      const dateEnd = toGet(this, dateEndStr)
      return dateStart && dateEnd && [new Date(dateStart), new Date(dateEnd)]
    },
    set(arr) {
      const [dateStart, dateEnd] = arr || []
      toSet(this, dateStartStr, dateStart)
      toSet(this, dateEndStr, dateEnd)
    }
  }
}

/*------ computed 映射 end --------*/
